package com.app.TCES.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.app.TCES.interceptor.SingleUserInterceptor;


@Configuration
public class WebConfigurer implements WebMvcConfigurer {

	@Autowired
	private SingleUserInterceptor singleUserInterceptor;

	// 这个方法是用来配置静态资源，比如html，js，css，等等
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");		
	}

	// 这个方法用来注册拦截器，我们自己写好的拦截器需要通过这里添加注册才能生效
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		InterceptorRegistration addInterceptor = registry.addInterceptor(singleUserInterceptor);
		// 排除配置
		addInterceptor.excludePathPatterns("/Login");
        addInterceptor.excludePathPatterns("/css/**");//排除静态资源
        addInterceptor.excludePathPatterns("/fonts/**");
        addInterceptor.excludePathPatterns("/img/**");
        addInterceptor.excludePathPatterns("/js/**");
        addInterceptor.excludePathPatterns("/layui/**");
        addInterceptor.excludePathPatterns("/templates/**");
        addInterceptor.excludePathPatterns("/Checklogin");
        addInterceptor.excludePathPatterns("/Logout");
        // 拦截配置
        addInterceptor.addPathPatterns("/**");
	}

	public void addCorsMapping(CorsRegistry registry) {
		registry.addMapping("/**")//设置允许跨域的路径
				.allowedOrigins("*")//设置运行跨域请求的域名
				.allowCredentials(true)//是否允许证书，不在默认开启
				.allowedMethods("GET", "POST", "PUT", "DFLETE")//设置允许的方法
				.maxAge(3600);//跨域允许时间
	}
}
